//
//  KuPersonViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/18.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "KuPersonViewController.h"

#import "KLManView.h"
#import "PinJiaCell.h"
@interface KuPersonViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *datas;

@end

@implementation KuPersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBGColor];
    _datas = [NSMutableArray array];
    [self showNav:@"成员资料"];
    [self loadUI];
    [self loadData];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    KLManView *view = [[[NSBundle mainBundle] loadNibNamed:@"KLManView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 300);
    _tableView.tableHeaderView = view;
    
    view.images.layer.cornerRadius = 50.0;
    view.images.layer.masksToBounds = YES;
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[_dic objectForKey:@"img"]];
    [view.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@""]];
    
    view.labName.text = [_dic objectForKey:@"name"];
    NSString *age = [NSString stringWithFormat:@"%li岁",[[_dic objectForKey:@"age"] integerValue]];
    view.labAge.text = age;
    
    NSString *year = [NSString stringWithFormat:@"%li年",[[_dic objectForKey:@"seniority"] integerValue]];
    view.labYear.text = year;
    
    NSString *order = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"order"] integerValue]];
    view.labOrder.text = order;
    
    NSString *star = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"star"] integerValue]];
    NSInteger count = [star integerValue];
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    starView.showStar = count * 30;
    [view.starView addSubview:starView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
    }
    
    cell.dataDic = _datas[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 80, 20)];
    lab.text = @"总评价:";
    lab.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:lab];
    
    UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, 60, 20)];
    count.text = [NSString stringWithFormat:@"%ld",_datas.count];
    count.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:count];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(16, 39, ScreenWidth-32, 0.5)];
    line.backgroundColor = [UIColor grayColor];
    [view addSubview:line];
    
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

- (void)loadData{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *merid = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"id"] integerValue]];
    [parmas setObject:merid forKey:@"id"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Coolie/comment",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        [_datas addObjectsFromArray:responseObject[@"result"][@"comment"]];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
